package com.example.review;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;


import com.example.review.recycleView.Fruit;
import com.example.review.recycleView.FruitAdapter2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 实现成瀑布流布局
 */
public class chapterThree3Activity extends AppCompatActivity {

    public List<Fruit> fruitList =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_three3);
        initFruit(); // 初始化水果数据
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        //第一个参数用于指定布局 的列数，传入3表示会把布局分为3列；
        // 第二个参数用于指定布局的排列方向，传 入StaggeredGridLayoutManager.VERTICAL 表示会让布局纵向排列
        StaggeredGridLayoutManager staggeredGridLayoutManager=
                new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        FruitAdapter2 adapter = new FruitAdapter2(fruitList);
        recyclerView.setAdapter(adapter);
    }

    private void initFruit(){

        for (int i = 0; i <5 ; i++) {
            Fruit apple =new Fruit(getRandomLengthName("红苹果"),R.drawable.tx);
            fruitList.add(apple);
            Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.tx);
            fruitList.add(banana);
            Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.tx);
            fruitList.add(orange);
            Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.tx);
            fruitList.add(watermelon);
            Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.tx);
            fruitList.add(pear);
        }
    }


    /**
     * 让名字随机重复1到20次  瀑布流布局 需要各个子项的高度不一致才能看出明显的效果
     * @param name 水果名字
     * @return
     */
    private String getRandomLengthName(String name) {
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(name);
        }
        return builder.toString();
    }
}







/**
 * 垂直和水平
 */
/*
public class chapterThree3Activity extends AppCompatActivity {

    public List<Fruit> fruitList =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_three3);
        initFruit(); // 初始化水果数据
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        //这里使用的LinearLayoutManager是线性布局的意思，可以实现和 ListView类似的效果
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //将其设置为水平
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter2 adapter = new FruitAdapter2(fruitList);
        recyclerView.setAdapter(adapter);
    }

    private void initFruit(){

        for (int i = 0; i <5 ; i++) {
            Fruit apple =new Fruit("红苹果",R.drawable.tx);
            fruitList.add(apple);
            Fruit banana = new Fruit("Banana", R.drawable.tx);
            fruitList.add(banana);
            Fruit orange = new Fruit("Orange", R.drawable.tx);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("Watermelon", R.drawable.tx);
            fruitList.add(watermelon);
            Fruit pear = new Fruit("Pear", R.drawable.tx);
            fruitList.add(pear);
        }
    }
}*/
